package com.example.projectjava.controller;

import com.example.projectjava.dao.UserDao;
import com.example.projectjava.service.AddressService;
import com.example.projectjava.service.UserService;
import com.example.projectjava.util.JsonResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@Tag(name = "地址管理接口")
public class AddressController {
    @Autowired   //此注解的作用：是spring的基本特性，它将项目中运行过程中的JdbcTemplate,自动创建并实例化传入
    JdbcTemplate jdbcTemplate;

    @Autowired
    AddressService addresSvice;

    @ResponseBody
    @GetMapping("/address")
    //求前端传递  帐号，密码，确认密码，出生日期，手机号五个参数

    @Operation(summary = "插入地址")
    @Parameters({
            @Parameter(name ="address_id",description="地址id(不允许为空)",in = ParameterIn.QUERY,required = false),
            @Parameter(name ="recipient",description="收件人(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="address",description="收件地址(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="phone",description="手机号(不允许为空)",in = ParameterIn.QUERY,required = true),
    })
    public JsonResponse insertAddress(String address_id,    //注册条件判断
                                     String recipient,
                                     String address,
                                     String phone) {
        JsonResponse jsonResponse = new JsonResponse();
        addresSvice.insertAddress(address_id, recipient, address, phone);    //调用service层的userRegister方法判断用户名手机号是否重复
        jsonResponse.setCode(0);
        jsonResponse.setMsg("插入成功");
        return jsonResponse;
    }


    @ResponseBody
    @GetMapping("/deleteAddress")
    @Operation(summary = "删除地址")
    @Parameters({
            @Parameter(name ="address_id",description="地址id(不允许为空)",in = ParameterIn.QUERY,required = false)

    })
    public JsonResponse deleteAddress(String address_id) {
        JsonResponse jsonResponse = new JsonResponse();
        try {
            addresSvice.deleteAddress(address_id);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg("删除失败:" + e.getMessage());
        }
        return jsonResponse;
    }

    @ResponseBody
    @GetMapping("/queryAllAddress")
    @Operation(summary = "查询所有地址信息")
    public JsonResponse queryAllAddress() {
        JsonResponse jsonResponse = new JsonResponse();
        List<Map<String, Object>> addressList = addresSvice.queryAllAddress();    //调用service层的queryAllUser方法查询所有用户信息
        jsonResponse.setCode(0);
        jsonResponse.setMsg("查询成功");
        jsonResponse.setData(addressList);
        return jsonResponse;
    }


    @ResponseBody
    @GetMapping("/updateAddress")
    @Operation(summary = "更新地址信息")
    @Parameters({
            @Parameter(name ="address_id",description="地址id(不允许为空)",in = ParameterIn.QUERY,required = false),
            @Parameter(name ="recipient",description="收件人(不允许为空)",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="address",description="收件地址",in = ParameterIn.QUERY,required = true),
            @Parameter(name ="phone",description="手机号",in = ParameterIn.QUERY,required = true)

    })
    public JsonResponse updateAddress(String address_id, String recipient, String address, String phone) {
        JsonResponse jsonResponse = new JsonResponse();
        try {
            addresSvice.updateAddress(address_id, recipient, address, phone);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg("更新失败:" + e.getMessage());
        }
        return jsonResponse;
    }
}
